package com.lagou.xsq_springboot_rpc_provider.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 获取 spring bean 的工具类
 * @author xsq
 *
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {

    private static final Logger logger= LoggerFactory.getLogger(SpringContextUtils.class);

	private static ApplicationContext context;
	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		SpringContextUtils.context = context;
        logger.info("---------SpringContextUtils 启动完成--------------------");
	}
	public static <T> T getBean(String beanId, Class<T> clazz) {
		return context.getBean(beanId, clazz);
	}
	
	public static <T> T getBean(Class<T> clazz) {
		return context.getBean(clazz);
	}

	public static ApplicationContext getContext() {
		return context;
	}
}